package com.jt.redis;

import org.junit.Test;
import redis.clients.jedis.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by zah on 2017/6/16.
 */
public class RedisTest01 {

    //清空三个redis服务器上的缓存数据
    @Test
    public void clear() {
        //设置redis服务器的ip和端口
        Jedis jedis1 = new Jedis("192.168.136.40", 6379);
        Jedis jedis2 = new Jedis("192.168.136.40", 6380);
        Jedis jedis3 = new Jedis("192.168.136.40", 6381);
        jedis1.flushAll();
        jedis2.flushAll();
        jedis3.flushAll();
        jedis1.close();
        jedis2.close();
        jedis3.close();
    }

    //测试java连接redis
    @Test
    public void test01() {
        //设置redis服务器的ip和端口
        Jedis jedis = new Jedis("192.168.136.40", 6379);
        //一次设置一个值
        jedis.set("name", "zhou");
        System.out.println(jedis.get("name"));

        //批量设值
        jedis.mset("t1", "1", "t2", "2");
        List<String> list = jedis.mget("t1", "t2");
        for (String v : list) {
            System.out.println(v);
        }
        jedis.close();
    }

    //测试连接池(一个redis实例)
    @Test
    public void test02() {
        // 构建连接池配置信息
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 设置最大连接数
        jedisPoolConfig.setMaxTotal(200);
        // 构建连接池
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,
                "192.168.136.40", 6379);

        // 从连接池中获取连接
        Jedis jedis = jedisPool.getResource();
        // 读取数据
        System.out.println(jedis.get("name"));
        // 将连接还回到连接池中
        jedisPool.returnResource(jedis);
        // 释放连接池
        jedisPool.close();
    }

    @Test //分片(三个redis实例)
    public void test03() {
        //构造各个节点链接信息，host 和 port
        List<JedisShardInfo> infoList = new ArrayList<>();
        JedisShardInfo info1 = new JedisShardInfo("192.168.136.40", 6379);
        JedisShardInfo info2 = new JedisShardInfo("192.168.136.40", 6380);
        JedisShardInfo info3 = new JedisShardInfo("192.168.136.40", 6381);
        //info1.setPassword("123456");
        infoList.add(info1);
        infoList.add(info2);
        infoList.add(info3);

        //分片 jedis
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(500); //最大链接数
        ShardedJedisPool pool = new ShardedJedisPool(config, infoList);
        //ShardedJedis jedis = new ShardedJedis(infoList);
        ShardedJedis jedis = pool.getResource();//从 pool 中获取

        //先清空这个三个redis实例，可以观察存值时的分布情况。
        for (int i = 0; i < 100; i++) {
            jedis.set("n" + i, "t" + i);
        }
        System.out.println(jedis.get("n9"));
        jedis.close();
    }


    //测试docker容器中的redis
    @Test
    public  void test04(){
        Jedis jedis = new Jedis("192.168.136.88", 7001);
        //jedis.auth("123456");
        //jedis.set("name", "zang"); //调用 redis 命令 set
        String s = jedis.get("name");
        System.out.println(s);
        jedis.close();
    }

    @Test
    public  void test05(){
        //构造各个节点链接信息，host 和 port
        List<JedisShardInfo> infoList = new
                ArrayList<JedisShardInfo>();
        JedisShardInfo info1 = new
                JedisShardInfo("192.168.136.88",7000);
        //info1.setPassword("123456");
        infoList.add(info1);
        JedisShardInfo info2 = new
                JedisShardInfo("192.168.136.88",7001);
        infoList.add(info2);
        JedisShardInfo info3 = new
                JedisShardInfo("192.168.136.88",7002);
        infoList.add(info3);
        //分片 jedis
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(500); //最大链接数
        ShardedJedisPool pool = new ShardedJedisPool(config, infoList);
        //ShardedJedis jedis = new ShardedJedis(infoList);
        ShardedJedis jedis = pool.getResource();//从 pool 中获取
        for(int i=0;i<100;i++){
            jedis.set("n"+i, "t"+i);
        }
        System.out.println(jedis.get("n9"));
        jedis.close();
    }

    @Test
    public void sentinel(){
        Set<String> sentinels = new HashSet<String>();
        sentinels.add(new
                HostAndPort("192.168.136.41",26379).toString());
        sentinels.add(new
                HostAndPort("192.168.136.41",26380).toString());
        sentinels.add(new
                HostAndPort("192.168.136.41",26381).toString());
        JedisSentinelPool pool = new JedisSentinelPool("mymaster",
                sentinels);
        System.out.println("当前 master：" +
                pool.getCurrentHostMaster());
        Jedis jedis = pool.getResource();
        //jedis.auth("123456");
        System.out.println(jedis.get("num"));
        pool.returnResource(jedis);
        pool.destroy();
        System.out.println("ok");
    }



}
